#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

# Check if user is root
if [ $(id -u) != "0" ]; then
    echo "Error: You must be root to run this script, please use root to install lnmp"
    exit 1
fi

cur_dir=$(pwd)
[[ -s /root/lnmp-install.log ]] && mv /root/lnmp-install.log /root/lnmp-install.last.log
[[ -s /root/moodle-install.log ]] && mv /root/moodle-install.log /root/moodle-install.last.log

#Stack="lnmp"

. lnmp.conf
. include/main.sh
. include/mysql.sh
. include/mariadb.sh
. include/php.sh
. include/nginx.sh
. include/apache.sh
. include/moodle.sh
. include/memcached.sh

Install_DB()
{
    if [ "${DBSelect}" = "1" ]; then
        Install_MySQL_51
     elif [ "${DBSelect}" = "2" ]; then
        Install_MySQL_55
     elif [ "${DBSelect}" = "3" ]; then
        Install_MySQL_56
     elif [ "${DBSelect}" = "4" ]; then
        Install_Boost
        Install_MySQL_57
     elif [ "${DBSelect}" = "5" ]; then
        Install_MySQL_8
     elif [ "${DBSelect}" = "6" ]; then
        Install_MariaDB_5
     elif [ "${DBSelect}" = "7" ]; then
        Install_MariaDB_10
     else
        Install_gcc
        Install_cmake
        Install_MariaDB_10
    fi
}

Install_PHP()
{
    if [ "${PHPSelect}" = "1" ]; then
        Install_PHP_52
    elif [ "${PHPSelect}" = "2" ]; then
        Install_PHP_53
    elif [ "${PHPSelect}" = "3" ]; then
        Install_PHP_54
    elif [ "${PHPSelect}" = "4" ]; then
        Install_PHP_55
    elif [ "${PHPSelect}" = "5" ]; then
        Install_PHP_56
    else
        Install_PHP_7
    fi
}

LNMP_Stack()
{
    Press_Install
    Print_APP_Ver
    Print_Sys_Info
    Check_Hosts
    Check_Mirror
    if [ "${DISTRO}" = "CentOS" ]; then
        CentOS_Modify_Source
     elif [ "${DISTRO}" = "RHEL" ]; then
        RHEL_Modify_Source
     elif [ "${DISTRO}" = "Ubuntu" ]; then
        Ubuntu_Modify_Source
     elif [ "${DISTRO}" = "Debian" ]; then
        Debian_Modify_Source
    fi
    Get_Dist_Version
    Set_Timezone
    if [ "$PM" = "yum" ]; then
        CentOS_InstallNTP
        CentOS_RemoveAMP
        CentOS_Dependent
     elif [ "$PM" = "apt" ]; then
        Deb_InstallNTP
        Xen_Hwcap_Setting
        Deb_RemoveAMP
        Deb_Dependent
    fi
    Disable_Selinux
    Check_Download
    Install_Autoconf
    Install_Libiconv
    [[ ${PHPSelect} -lt 8 ]] && Install_Libmcrypt
    Install_Libxsl
    Install_Libtidy
    Install_Mhash
    [[ ${PHPSelect} -lt 8 ]] && Install_Mcrypt
    Install_Freetype
    Install_Curl
    Install_Pcre
    Install_Icu4c
    if [ "${SelectMalloc}" = "2" ]; then
        Install_Jemalloc
     elif [ "${SelectMalloc}" = "3" ]; then
        Install_TCMalloc
    fi
    if [ "$PM" = "yum" ]; then
        CentOS_Lib_Opt
     elif [ "$PM" = "apt" ]; then
        Deb_Lib_Opt
        Deb_Check_MySQL
    fi
    [[ "$isDB" != "ok" ]] && Install_DB
    [[ "$isPHP" != "ok" ]] && Export_PHP_Autoconf
    [[ "$isPHP" != "ok" ]] && Install_PHP
    [[ "$isPHP" != "ok" ]] && LNMP_PHP_Opt
    [[ ( "${NginxSelect}" = "3" || "${NginxSelect}" = "4" ) && "$isNginx" != "ok" ]]  && Install_Memcached 2
    [[ "$isNginx" != "ok" ]] && Install_Nginx
    Creat_PHP_Tools
    Add_LNMP_Startup
    Check_LNMP_Install
}


Get_Dist_Name
if [ "${DISTRO}" = "unknow" ]; then
    Echo_Red "Unable to get Linux distribution name, or do NOT support the current distribution."
    exit 1
fi
clear
echo "+------------------------------------------------------------------------+"
echo "|           Moodle for ${DISTRO} Linux Server, Written by hongm         |"
echo "+------------------------------------------------------------------------+"
echo "|         A tool to auto-compile & install LNMP+Moodle on Linux          |"
echo "+------------------------------------------------------------------------+"
echo "|        For more information please visit http://hongm.emoodle.org      |"
echo "+------------------------------------------------------------------------+"
Check_Stack
if [[ "${Get_Stack}" = "unknow" ]]; then
    Stack="lnmp"
    Check_Nginx_Files
    Check_DB_Files
    Check_PHP_Files
    echo "Moodle operating environment is not installed!"
    echo "Start install ${Stack} V${LNMP_Ver} and Moodle...... or Press Ctrl+c to cancel"
    if [[ "$isDB" != "ok" ]]; then
        Set_DB_Password
        Storage_Selection
        DB_Selection
     else
        Verify_DB_Password
    fi
    [[ "$isPHP" != "ok" ]] && PHP_Selection
    Malloc_Selection
    [[ "$isNginx" != "ok" ]] && Nginx_Selection
    Moodle_Selection
    LNMP_Stack 2>&1 | tee -a /root/lnmp-install.log
 else
    Stack=${Get_Stack}
    echo -e " \e[0;31mMoodle operating environment is ${Stack}.\e[0m"
    Verify_DB_Password
    Moodle_Selection
    Start_Moodle
fi
echo ${Stack}
Install_Moodle 2>&1 | tee -a /root/moodle-install.log
Print_Moodle_Info 2>&1 | tee -a /root/moodle-install.log
